package com.jess.arms.base.my;

import android.net.ParseException;
import android.os.Build;
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.util.SparseIntArray;
import android.util.SparseLongArray;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonParseException;
import com.jess.arms.mvp.IView;
import com.jess.arms.utils.LogUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Array;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;
import java.util.Collection;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.ResponseBody;
import retrofit2.HttpException;

/**
 * alex
 *
 * @param <T>
 */
public abstract class BaseLoadingLayoutSubscriber<T> extends BaseSubscriber<T> {


    private final IView mRootView;

    public BaseLoadingLayoutSubscriber(IView mRootView) {
        super(mRootView);
        this.mRootView = mRootView;
    }

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(BaseEntity<T> tBaseEntity) {
        if (tBaseEntity.isSuccess()) {
            onSuccess(tBaseEntity);
        } else {
            onRequestError(tBaseEntity.getMsg());
        }
    }

    public void onSuccess(BaseEntity<T> tBaseEntity) {
        if (isNullString(tBaseEntity.getData().toString())) {
            mRootView.showEmpty();
            return;
        }
        if (isEmpty(tBaseEntity.getData())) {
            mRootView.showEmpty();
            return;
        }
        mRootView.showContent();
        onRequestSuccess(tBaseEntity.getData());
    }

    ;

    @Override
    public void onError(Throwable t) {
        LogUtils.debugInfo(t.toString());
        if (t instanceof UnknownHostException || t instanceof ConnectException || t instanceof UnknownServiceException) {
            mRootView.showNoNetwork();
        } else if (t instanceof SocketTimeoutException) {
            mRootView.showTimeOut();
        } else if (t instanceof HttpException) {
            mRootView.showFailure();
        } else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
            mRootView.showFailure();
        } else {
            mRootView.showFailure();
        }
        onRequestError(t.getMessage());
    }

    @Override
    public void onComplete() {

    }

    /**
     * 判断对象是否为空
     *
     * @param obj 对象
     * @return {@code true}: 为空<br>{@code false}: 不为空
     */
    private boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        }
        if (obj instanceof String && obj.toString().length() == 0) {
            return true;
        }
        if (obj.getClass().isArray() && Array.getLength(obj) == 0) {
            return true;
        }
        if (obj instanceof Collection && ((Collection) obj).isEmpty()) {
            return true;
        }
        if (obj instanceof Map && ((Map) obj).isEmpty()) {
            return true;
        }
        if (obj instanceof SparseArray && ((SparseArray) obj).size() == 0) {
            return true;
        }
        if (obj instanceof SparseBooleanArray && ((SparseBooleanArray) obj).size() == 0) {
            return true;
        }
        if (obj instanceof SparseIntArray && ((SparseIntArray) obj).size() == 0) {
            return true;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            if (obj instanceof SparseLongArray && ((SparseLongArray) obj).size() == 0) {
                return true;
            }
        }
        return false;
    }


    /**
     * 判断字符串是否为空 为空即true
     *
     * @param str 字符串
     * @return
     */
    public static boolean isNullString(@Nullable String str) {
        return str == null || str.length() == 0 || "".equals(str) || "null".equals(str);
    }

}
